// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin.chips;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public final class Incrementor3 implements Chip {
  public final IntBus2 ADDRESS = new IntBus2();
  public final ByteBus2 DATA = new ByteBus2();
  public final Pin2 WR = new Pin2();
  public final Pin2 REQ = new Pin2();
  
  private int address = 0;
  private boolean read = false;
  private int counter;
  
  public Incrementor3(boolean runFirst) {
    counter = runFirst ? 0 : 2;
  }
  
  public void update() {
    if ((counter++ & 0x02) == 0) {
      if (read) {
        ADDRESS.set(address);
        DATA.reset();
        WR.set(false);
        REQ.set(true);
      } else {
        ADDRESS.set(address++);
        DATA.set(DATA.get());
        WR.set(true);
        REQ.set(true);
      }
      read = !read;
    } else {
      ADDRESS.reset();
      DATA.reset();
      WR.reset();
      REQ.reset();
    }
  }
}
